// pages/checkout/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data: [],
    loading: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ extConfig: app.getExtConfigSync() })
    this.data.order_sn = options.order_sn ? options.order_sn : 'I9089087535110d';
    this.loadOrder();
  },

  //删除订单
  del: function (e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确认移除吗？',
      success: function (res) {
        if (res.confirm) {
          //删除评论
          app.api("mall/order_offline/del", {
            id: id,
            method: 'POST'
          }, function (res) {
            //删了出错
            if (res.code != 200) {
              app.alert(res.msg);
            } else {
             wx.navigateBack()
            }
          });
        }
      }
    })
  },


  show_popup3: function (e) {
    var order_sn = e.currentTarget.dataset.order_sn;
    this.data.shop_id = e.currentTarget.dataset.shop_id;
    this.setData({
      popup3: 1,
      order_sn: order_sn,
      refund_money: e.currentTarget.dataset.refund_money,
    })
  },
  btn_cancel3: function () {
    this.setData({
      popup3: 0,
    })
  },
  //退款
  updateStatus_16: function (e) {
    var post = e.detail.value;
    var that = this;
    that.setData({
      loading: 1,
      disabled: 1
    });
    if (post.remark == "") {
      app.alert('请输入备注!');
      return;
    }
    if (post.refund_money == "") {
      app.alert('请输入退回金额!');
      return;
    }
    app.api('/mall-admin/mall-order-offline/updateStatus_16', {
      order_sn: this.data.order_sn,
      refund_desc: post.remark,
      refund_money: post.refund_money,
      shop_id: this.data.shop_id,
      //  method: 'POST',
    }, function (data) {
      if (data.error == 0) {
        wx.showToast({
          title: '操作成功',
        })
        that.btn_cancel3();
        that.loadOrder(1);
      } else {
        app.alert(data.msg);
        that.setData({
          loading: 0,
          disabled: 0
        });
      }
    })
  },



  //出餐中
  updateStatus_2(e) {
    var order_sn = e.currentTarget.dataset.order_sn;
    var shop_id = e.currentTarget.dataset.shop_id;
    var that = this;
    app.api('/mall-admin/mall-order-offline/updateStatus_5', {
      order_sn: order_sn,
      shop_id: shop_id
    }, function (data) {
      if (data.error == 0) {
        wx.showToast({
          title: '操作成功',
        })
        that.loadOrder(1);
      } else {
        app.alert(data.msg);
      }
    })
  },



  //出餐完成
  updateStatus_5(e) {
    var order_sn = e.currentTarget.dataset.order_sn;
    var shop_id = e.currentTarget.dataset.shop_id;
    var that = this;
    app.api('/mall-admin/mall-order-offline/updateStatus_4', {
      order_sn: order_sn,
      shop_id: shop_id
    }, function (data) {
      if (data.error == 0) {
        wx.showToast({
          title: '操作成功',
        })
        that.loadOrder(1);
      } else {
        app.alert(data.msg);
      }
    })
  },

  //加载购物车
  loadOrder(reload = 0) {
    var that = this;
    if (reload == 1) {
      this.setData({
        loading: 0,
        listData: [],
      });
      app.setPageData("pages/bookOffline/index",{reload:1});
    }
    this.data.loading = 1;
    app.api("mall/order_offline/find", {
      order_sn: this.data.order_sn
    }, function (res) {
      that.data.loading = 0;
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      
      that.setData({
        data: res.data
      });
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
var reload = app.getPageData("group/index");
    if (reload==1){
      this.loadOrder();
      app.setPageData("group/index", { reload: 0 });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  suc: function () {
    wx.navigateTo({
      url: '/pages/order/index',
    })
  },
  

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})